﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.IO;
using University.Workshop.Model;
using System.Web.UI.HtmlControls;
using System.Globalization;
using University.Workshop.DataAccess;

namespace University.Workshop.WebApp
{
    public partial class RegisterComment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            else
            {
                Topic topic = DataAccess.TopicDataAccess.SelectTopicById(Convert.ToInt64(Request["title"], CultureInfo.CurrentCulture));
                TopicText.Text = topic.Title;
                LabelUser.Text = User.Identity.Name;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                
                HttpFileCollection uploadFilCol = Request.Files;
                HttpPostedFile file=null;
                for (int i = 0; i < uploadFilCol.Count; i++)
                {
                    file = uploadFilCol[i];
                }
                string fileExt = Path.GetExtension(file.FileName).ToLower(CultureInfo.CurrentCulture);
                if (fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".bmp" && fileExt != ".jpeg" && fileExt != ".png")
                    
                    Label1.Text = "file you are trying to upload aren't image";
                Comment comment = Business.RegisterCommentManager.RegisterComment(User.Identity.Name, textbox.Text, Request["title"]);
                ImageUploadHandler.UploadCommentImage(file, Server, comment.CommentId);
                 Response.Redirect("Comments.aspx?topicId="+comment.TopicId);
            }
            catch(Exception er)
            {
                Label1.Text=er.Message;
            }
        }
    }
}